#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
交易 - 获取OpenID功能使用示例
"""

import os
import sys

# 添加项目根目录到Python路径，确保能够导入jlpay包
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from examples.base_example import BaseExample
from jlpay.services.trans.common.get_openid_service import GetOpenidService
from jlpay.model.trans.common.common_trans import (
    GetOpenidRequest, GetOpenidResponse
)


class GetOpenidExample(BaseExample):
    """获取OpenID示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.trans.common.get_openid")
        self.get_openid_service = GetOpenidService(self.config)
    
    def build_get_openid_request(self):
        """
        构建获取OpenID请求
        
        Returns:
            GetOpenidRequest实例
        """
        request = GetOpenidRequest()
        
        # 设置请求参数
        request.set_mch_id("849584358120018")
        request.set_pay_type("wxpay")  # 微信支付
        request.set_auth_code("131208648101095993")
        request.set_sub_appid("wxd930ea53421a258f4f")
        request.set_app_up_identifier("test_identifier")
        request.set_device_info("N50007A8890")
        request.set_mch_create_ip("127.0.0.1")
        
        return request
    
    def run_example(self):
        """运行获取OpenID示例"""
        self.logger.info("开始执行获取OpenID示例")
        
        # 创建获取OpenID请求
        request = self.build_get_openid_request()
        
        # 发送请求并处理响应
        response = self.send_request_and_handle_response(
            self.get_openid_service.get_openid,  # 服务方法
            "获取OpenID",                        # 操作名称
            request                              # 请求参数
        )
        
        # 处理响应结果
        if response and response.is_success():
            self.logger.info(f"响应报文：{response.to_dict()}")
        
        self.logger.info("获取OpenID示例执行完毕")


def main():
    """主函数"""
    example = GetOpenidExample()
    example.run_example()


if __name__ == "__main__":
    main() 